﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WebApiSelfHost.Handlers
{
    public class MessageRequestHandler :  DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            bool isBadRequest = false; 
            if (isBadRequest) 
            { 
                return Task<HttpResponseMessage>.Factory.StartNew(() => 
                { 
                    return request.CreateResponse(HttpStatusCode.BadRequest); 
                });
            } 

            return base.SendAsync(request, cancellationToken)
                .ContinueWith((task) =>
                {
                    return task.Result;                     
                }); 
        }
    }
}
